Return the count of the number smaller than n


Posted by Christy on 2022-04-19

Description: Write a function named findSmallCount that accepts an array and a number n, returns the count of the number smaller than n.

function findSmallCount(arr, n) {

}

findSmallCount([1, 2, 3], 2) // 1
findSmallCount([1, 2, 3, 4, 5], 0) // 0
findSmallCount([1, 2, 3, 4], 100) // 4
function findSmallCount(arr, n) {
  let counter = 0;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < n) {
      counter += 1;
    }
  }
  return counter;
}

console.log(findSmallCount([1, 2, 3], 2)); // 1
console.log(findSmallCount([1, 2, 3, 4, 5], 0)); // 0
console.log(findSmallCount([1, 2, 3, 4], 100)); // 4

Reflection:

Beware of what the output is, here's the count.

The other answers:

function findSmallCount(arr, n) {
  let i = 0;
  let counter = 0;
  do {
    if (arr[i] < n) {
      counter++;
    }
    i++;
  } while (i < arr.length);
  return counter;
}
function findSmallCount(arr, n) {
  let i = 0;
  let counter = 0;
  while (i < arr.length) {
    if (arr[i] < n) {
      counter++;
    }
    i++;
  }
  return counter;
}









Related Posts

[ Day 05 ] 用 Puppeteer 來做自動化機器人吧 (四) : Dockerize 篇

[ Day 05 ] 用 Puppeteer 來做自動化機器人吧 (四) : Dockerize 篇

day01 安裝android studio + 認識adb

day01 安裝android studio + 認識adb

CSS 衍生的資安問題(中) - 不用 Hot Update 也行

CSS 衍生的資安問題(中) - 不用 Hot Update 也行


Comments